package cn.pegasus.teamwork.appinterface.controller.sdk;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.ObjectUtil;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.sysconfig.entity.SysConfigDictionary;
import cn.pegasus.teamwork.sysconfig.service.SysConfigDictionaryService;

@Controller
@RequestMapping("/help/*")
public class HomeController {

    private static final Logger LOGGER = Logger.getLogger(HomeController.class);

    @Resource
    private SysConfigDictionaryService dictionaryService;

    @RequestMapping("/getBankCardType")
    @ResponseBody
    public Map<String, Object> getBankCardType(@RequestParam Map<String, Object> map) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        try {
            String value = map.get("bankCard").toString().substring(0, 6);
            SysConfigDictionary dictionary = dictionaryService.getDic(1, value);
            if (dictionary != null) {
                resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, ObjectUtil.getObjectByFileNames(new String[] { "dictName", "id" }, dictionary));
            } else {
                resultMap = ReMessage.resultBack(ParameterEunm.ERROR_NOT_CARD_TYPE, null);
            }
        } catch (Exception e) {
            LOGGER.error("Server Request Error! ", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_PARAMS_CODE, null);
        }
        return resultMap;
    }

    @RequestMapping("/bankCardList")
    @ResponseBody
    public Map<String, Object> bankCardList(@RequestParam Map<String, Object> map) {
        LOGGER.debug(map);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<SysConfigDictionary> dictionary = dictionaryService.getDicList(1);
            if (dictionary != null) {
                resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, ObjectUtil.getListByFileNames(new String[] { "dictName", "id" }, dictionary));
            } else {
                resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
            }
        } catch (Exception e) {
            LOGGER.error("Server Request Error! ", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_PARAMS_CODE, null);
        }
        return resultMap;
    }

    @RequestMapping("/index")
    public ModelAndView index(@RequestParam Map<String, Object> map) {
        ModelAndView andView = new ModelAndView("/app/index");
        return andView;
    }

}
